package com.gxa.employment.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gxa.employment.entity.pojo.Role;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


/**
 * <p>
 * 系统角色表 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2025-03-25
 */
public interface RoleMapper extends BaseMapper<Role> {

    //删除该角色所有权限
    Integer deleteAll(Integer roleId);

    //删除特定类型的权限权限
    @Delete("DELETE FROM tb_rp_role WHERE rp_role_id=#{roleId} AND rp_perm_id IN (SELECT perm_id FROM tb_permission WHERE perm_type = #{type})")
    Integer deletePermByType(@Param("roleId") Integer roleId,@Param("type")String type);

    //添加该角色所有分配的权限
    Integer insertAll(@Param("list") List<Integer> list, @Param("roleId") Integer roleId);

    //根据角色查询权限
    //@Select("SELECT rp_perm_id FROM tb_rp_role tb1,tb_permission tb2 WHERE tb1.rp_perm_id = tb2.perm_id AND rp_role_id = #{roleId} AND perm_type = #{type}")
    List<Integer> selectByRole(@Param("roleId") Integer roleId,@Param("type")String type);

    //为管理员分配权限
    @Insert("INSERT INTO tb_manager_role(manager_id, role_id) VALUES (#{managerId},#{roleId})")
    Integer allocateRole(@Param("managerId") Integer managerId, @Param("roleId") Integer roleId);

    //修改管理员权限
    @Update("UPDATE tb_manager_role SET role_id=#{roleId},updated=NOW() WHERE manager_id= #{managerId}")
    Integer updateRole(@Param("managerId") Integer managerId, @Param("roleId") Integer roleId);

    //查询当前管理员的角色id
    @Select("SELECT role_id FROM tb_manager_role WHERE manager_id=#{id}")
    Integer selectByManager(Integer id);

    //为角色添加权限
    @Insert("INSERT INTO tb_rp_role(rp_role_id,rp_perm_id) VALUES(#{roleId},#{permId});")
    Integer addPermissionForRole(@Param("roleId")Integer roleId, @Param("permId")Integer permId);

    @Delete("DELETE FROM tb_rp_role WHERE rp_perm_id=#{permId} AND rp_role_id=#{roleId}")
    void deletePermissionForRole(@Param("roleId")Integer roleId, @Param("permId")Integer permId);
}
